home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / Xprof / xmeasure / tsttxt.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  11KB  |  451 lines

  1. /*==================================================================
  2.  *      File :          tsttxt.c
  3.  *      Package:        Xmeasure
  4.  * 
  5.  *      Author :        Aloke Gupta.
  6.  *
  7.  *  (C) Copyright 1992, Aloke Gupta.
  8.  *  All rights granted to University of Illinois Board of Regents.
  9.  *==================================================================*/
  10.  
  11. #include "perf.h"
  12.  
  13. /* Shared variables */
  14. static int       i;
  15. static Font       font;
  16. static XFontStruct *fontstruct;
  17. static GC       fontgc;
  18. static int       fontheight;
  19.  
  20. /* The following definition may be needed for DECWindows server */
  21. /*
  22. static char *FontName[]  = {"times_roman10", "times_roman8", "times_roman12"};
  23. */
  24. char *FontName[]  = {
  25.     "6x13",  "8x13",   "9x15",    NULL,     NULL,     NULL,     NULL,     NULL,
  26.     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,
  27.     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,
  28.     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,     NULL,
  29. };
  30. unsigned NUMFONTS = sizeof (FontName) / sizeof (char *);
  31.  
  32. #define FIRSTASCII    ' '
  33. #define LASTASCII    127
  34. #define XPOS     20
  35.  
  36. /* ====================================================================== */
  37. int   OpenFont(xd, xp)       
  38. XD *xd;
  39. XParams *xp;
  40. {
  41.     for (i = 0; i < xp-> itns; i ++)
  42.     XLoadFont(xd->display, FontName[0]);
  43. }
  44. int i_OpenFont(xd, xp)       
  45. XD *xd;
  46. XParams *xp;
  47. { }
  48. int c_OpenFont(xd, xp)       
  49. XD *xd;
  50. XParams *xp;
  51. { }
  52.  
  53. /* ====================================================================== */
  54.  
  55. int   CloseFont(xd, xp)      
  56. XD *xd;
  57. XParams *xp;
  58. {
  59.     for (i = 0; i < xp-> itns; i ++)
  60.     XUnloadFont(xd->display, font);
  61. }
  62.  
  63. int i_CloseFont(xd, xp)      
  64. XD *xd;
  65. XParams *xp;
  66. {
  67.     font = XLoadFont(xd->display, FontName[0]);
  68.     fontgc = XCreateGC(xd->display, xd->window, 0, 0);
  69.     XSetFont(xd->display, fontgc, font);
  70. }
  71.  
  72. int c_CloseFont(xd, xp)      
  73. XD *xd;
  74. XParams *xp;
  75. {   XFreeGC(xd->display, fontgc); }
  76.  
  77. /* ====================================================================== */
  78. static XFontStruct *font_struct;
  79. static XFontStruct **font_struct_array;
  80.  
  81. int   QueryFont(xd, xp)      
  82. XD *xd;
  83. XParams *xp;
  84. {
  85.     for (i = 0; i < xp-> itns; i ++)
  86.         font_struct_array[i] = XQueryFont(xd->display, font);
  87. }
  88. int i_QueryFont(xd, xp)      
  89. XD *xd;
  90. XParams *xp;
  91. {
  92.   font = XLoadFont(xd->display, FontName[0]);
  93.   font_struct = XQueryFont(xd->display, font);
  94.   font_struct_array=(XFontStruct **) malloc (xp->itns * sizeof (XFontStruct *));
  95. }
  96. int c_QueryFont(xd, xp)      
  97. XD *xd;
  98. XParams *xp;
  99. {
  100.   XFreeFont(xd->display, font_struct);  /* Release the "FontName[0]" font */
  101.   for (i = 0; i < xp-> itns; i ++)
  102.       XFree(font_struct_array[i]);
  103.   free(font_struct_array);
  104. }
  105.  
  106. /* ====================================================================== */
  107.  
  108. int   QueryTextExtents(xd, xp)
  109. XD *xd;
  110. XParams *xp;
  111. {
  112.   int direction, ascent, descent;
  113.   XCharStruct overall;
  114.   char *string = "test string for QueryTextExtents";
  115.   int nchars;
  116.  
  117.   nchars = strlen(string);
  118.  
  119.   for (i = 0; i < xp-> itns; i ++)
  120.     XQueryTextExtents(xd->display, font, string, nchars, &direction,
  121.               &ascent, &descent, &overall);
  122. }
  123.  
  124. int i_QueryTextExtents(xd, xp)
  125. XD *xd;
  126. XParams *xp;
  127. {
  128.   font = XLoadFont(xd->display, FontName[0]);
  129. }
  130.  
  131. int c_QueryTextExtents(xd, xp)
  132. XD *xd;
  133. XParams *xp;
  134. { }
  135.  
  136. /* ====================================================================== */
  137. static char ***list_fonts;
  138. static int num_fonts;
  139. static char * list_font_string = "*misc*";
  140.  
  141. int   ListFonts(xd, xp)      
  142. XD *xd;
  143. XParams *xp;
  144. {
  145.   for (i = 0; i < xp-> itns; i ++)
  146.     list_fonts[i] = XListFonts(xd->display, list_font_string,
  147.                    MAXINT, &num_fonts);
  148. }
  149.  
  150. int i_ListFonts(xd, xp)      
  151. XD *xd;
  152. XParams *xp;
  153. {
  154.   list_fonts = (char ***) malloc(sizeof(char **) * xp->itns);
  155. }
  156.  
  157. int c_ListFonts(xd, xp)      
  158. XD *xd;
  159. XParams *xp;
  160. {
  161.   for (i = 0; i < xp-> itns; i ++)
  162.     XFreeFontNames(list_fonts[i]);
  163.   free(list_fonts);
  164. }
  165.  
  166.  
  167. /* ====================================================================== */
  168.  
  169. int   ListFontsWithInfo(xd, xp)
  170. XD *xd;
  171. XParams *xp;
  172. {
  173.   for (i = 0; i < xp-> itns; i ++)
  174.     list_fonts[i] = XListFontsWithInfo(xd->display, list_font_string,
  175.                        MAXINT, &num_fonts, &fontstruct);
  176. }
  177.  
  178. int i_ListFontsWithInfo(xd, xp)
  179. XD *xd;
  180. XParams *xp;
  181. {
  182.   list_fonts = (char ***) malloc(sizeof(char **) * xp->itns);
  183. }
  184.  
  185. int c_ListFontsWithInfo(xd, xp)
  186. XD *xd;
  187. XParams *xp;
  188. {
  189.   for (i = 0; i < xp-> itns; i ++)
  190.     XFreeFontNames(list_fonts[i]);
  191.   free(list_fonts);
  192. }
  193.  
  194. /* ====================================================================== */
  195. static char **font_path;
  196. int   SetFontPath(xd, xp)    
  197. XD *xd;
  198. XParams *xp;
  199. {
  200.   for (i = 0; i < xp-> itns; i ++)
  201.        XSetFontPath(xd->display, font_path, num_fonts);
  202. }
  203.  
  204. int i_SetFontPath(xd, xp)    
  205. XD *xd;
  206. XParams *xp;
  207. {
  208.   font_path = XGetFontPath(xd->display, &num_fonts);
  209. }
  210.  
  211. int c_SetFontPath(xd, xp)    
  212. XD *xd;
  213. XParams *xp;
  214. {
  215.   XFree(font_path);
  216. }
  217.  
  218. /* ====================================================================== */
  219.  
  220. int   GetFontPath(xd, xp)    
  221. XD *xd;
  222. XParams *xp;
  223. {
  224.   for (i = 0; i < xp-> itns; i ++)
  225.     list_fonts[i] = XGetFontPath(xd->display, &num_fonts);
  226. }
  227.  
  228. int i_GetFontPath(xd, xp)    
  229. XD *xd;
  230. XParams *xp;
  231. {
  232.   list_fonts = (char ***) malloc(sizeof(char **) * xp->itns);
  233. }
  234.  
  235. int c_GetFontPath(xd, xp)    
  236. XD *xd;
  237. XParams *xp;
  238. {
  239.   for (i = 0; i < xp-> itns; i ++)
  240.     XFree(list_fonts[i]);
  241.   free(list_fonts);
  242. }
  243.  
  244. /* ====================================================================== */
  245. /* PolyText8: Sizepair => (string_length, font_index)    */
  246.  
  247. static char **buffer;
  248.  
  249. int   PolyText8(xd, xp)      
  250. XD *xd;
  251. XParams *xp;
  252. {
  253.     int ypos = XPOS;
  254.  
  255.     for (i = 0; i < xp->itns; i++) {
  256.     XDrawString(xd->display, xd->window, fontgc, XPOS, ypos,
  257.         buffer[i], (int) xp->size);
  258.     ypos += fontheight; 
  259.     if (ypos > (DISPHEIGHT - fontheight)) 
  260.         ypos = XPOS;
  261.     }
  262. }
  263.  
  264. int i_PolyText8(xd, xp)      
  265. XD *xd;
  266. XParams *xp;
  267. {
  268.     XGCValues gcval;
  269.     int j, fontindex;
  270.     short aflag=0;
  271.     char c;
  272.  
  273.     xp->size    = xp->sizepair->s1;
  274.     xp->numobjs = xp->sizepair->s1;
  275.     fontindex   = xp->sizepair->s2;
  276.     if (fontindex < 0)           fontindex = 0;
  277.     else if (fontindex > NUMFONTS) fontindex = NUMFONTS;
  278.  
  279.     /* Load the font */
  280.     fontstruct = XLoadQueryFont(xd->display, FontName[fontindex]);
  281.     if (fontstruct == NULL) {
  282.     printf("Error: could not load font '%s'\n", FontName[fontindex]);
  283.     xp->itns = 0;
  284.     }
  285.     fontheight=fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent+1;
  286.     /* Create a graphics context for the text requests */
  287.     fontgc = XCreateGC(xd->display, xd->window, 0, 0);
  288.     gcval.font = fontstruct->fid;
  289.     XChangeGC(xd->display, fontgc, GCFont, &gcval);
  290.     XSetBackground(xd->display, fontgc, xd->background);
  291.     XSetForeground(xd->display, fontgc, xd->foreground);
  292.  
  293.     /* Setup strings to be drawn by the PolyText request */
  294.     buffer = (char **) malloc(xp->itns * sizeof(char *));
  295.     for (i = 0; i < xp->itns; i++) {
  296.     buffer[i] = (char *) malloc(xp->size * sizeof (char));
  297.     c = (char) (i + FIRSTASCII);
  298.     for (j = 0; j < xp->size; j++) {
  299.         buffer[i][j] = c;
  300.         if (c == LASTASCII) c = (char) FIRSTASCII;
  301.         else        c++;
  302.     }
  303.     }
  304.     while (XTextWidth(fontstruct, buffer[0], (int) xp->size) > DISPWIDTH) {
  305.     aflag = 1;
  306.     xp->size --;
  307.     }
  308.     if (aflag) fprintf(stderr, "# Warning: Adjusted string length to %d\n", xp->size);
  309.  
  310. }
  311.  
  312. int c_PolyText8(xd, xp)      
  313. XD *xd;
  314. XParams *xp;
  315. {
  316.     XFreeGC(xd->display, fontgc);
  317.     for (i = 0; i < xp->itns; i++)
  318.     free(buffer[i]);
  319.     free(buffer);
  320. }
  321.  
  322. /* ====================================================================== */
  323. /* PolyText16: Sizepair => (string_length, font_index)    */
  324. XChar2b **buffer16;
  325.  
  326. int   PolyText16(xd, xp)     
  327. XD *xd;
  328. XParams *xp;
  329. {
  330.     int ypos = XPOS;
  331.  
  332.     for (i = 0; i < xp->itns; i++) {
  333.     XDrawString16(xd->display, xd->window, fontgc, XPOS, ypos,
  334.         buffer16[i], xp->size);
  335.     ypos += fontheight; 
  336.     if (ypos > (DISPHEIGHT - fontheight)) 
  337.         ypos = XPOS;
  338.     }
  339. }
  340. int i_PolyText16(xd, xp)     
  341. XD *xd;
  342. XParams *xp;
  343. {
  344.     XGCValues gcval;
  345.     int j, fontindex;
  346.     short aflag=0;
  347.     char c;
  348.  
  349.     xp->size    = xp->sizepair->s1;
  350.     xp->numobjs = xp->sizepair->s1;
  351.     fontindex   = xp->sizepair->s2;
  352.     if (fontindex < 0)           fontindex = 0;
  353.     else if (fontindex > NUMFONTS) fontindex = NUMFONTS;
  354.  
  355.     /* Load the font */
  356.     fontstruct = XLoadQueryFont(xd->display, FontName[fontindex]);
  357.     if (fontstruct == NULL) {
  358.     printf("Error: could not load font '%s'\n", FontName[fontindex]);
  359.     xp->itns = 0;
  360.     }
  361.     fontheight=fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent+1;
  362.     /* Create a graphics context for the text requests */
  363.     fontgc = XCreateGC(xd->display, xd->window, 0, 0);
  364.     gcval.font = fontstruct->fid;
  365.     XChangeGC(xd->display, fontgc, GCFont, &gcval);
  366.     XSetBackground(xd->display, fontgc, xd->background);
  367.     XSetForeground(xd->display, fontgc, xd->foreground);
  368.  
  369.     /* Setup strings to be drawn by the PolyText request */
  370.     buffer16 = (XChar2b **) malloc(xp->itns * sizeof(XChar2b *));
  371.     for (i = 0; i < xp->itns; i++) {
  372.     buffer16[i] = (XChar2b *) malloc(xp->size * sizeof (XChar2b));
  373.     c = (char) (i + FIRSTASCII);
  374.     for (j = 0; j < xp->size; j++) {
  375.         buffer16[i][j].byte1  = (unsigned char) c;
  376.         buffer16[i][j].byte1  = (unsigned char) (LASTASCII - FIRSTASCII + c);
  377.         if (c == LASTASCII) c = (char) FIRSTASCII;
  378.         else        c++;
  379.     }
  380.     }
  381.     while (XTextWidth16(fontstruct, buffer16[0], (int) xp->size) > DISPWIDTH) {
  382.     aflag = 1;
  383.     xp->size --;
  384.     }
  385.     if (aflag) fprintf(stderr, "# Warning: Adjusted string length to %d\n", (int) xp->size);
  386.  
  387. }
  388. int c_PolyText16(xd, xp)     
  389. XD *xd;
  390. XParams *xp;
  391. {
  392.     XFreeGC(xd->display, fontgc);
  393.     for (i = 0; i < xp->itns; i++)
  394.     free(buffer16[i]);
  395.     free(buffer16);
  396. }
  397.  
  398. /* ====================================================================== */
  399. /* ImageText8: Sizepair => (string_length, font_index)    */
  400.  
  401. int   ImageText8(xd, xp)     
  402. XD *xd;
  403. XParams *xp;
  404. {   int ypos = XPOS;
  405.  
  406.     for (i = 0; i < xp->itns; i++) {
  407.     XDrawImageString(xd->display, xd->window, fontgc, XPOS, ypos,
  408.         buffer[i], (int) xp->size);
  409.     ypos += fontheight; 
  410.     if (ypos > (DISPHEIGHT - fontheight)) 
  411.         ypos = XPOS;
  412.     }
  413. }
  414.  
  415. int i_ImageText8(xd, xp)     
  416. XD *xd;
  417. XParams *xp;
  418. { i_PolyText8(xd, xp); }
  419.  
  420. int c_ImageText8(xd, xp)     
  421. XD *xd;
  422. XParams *xp;
  423. { c_PolyText8(xd, xp); }
  424.  
  425. /* ====================================================================== */
  426. /* ImageText8: Sizepair => (string_length, font_index)    */
  427.  
  428. int   ImageText16(xd, xp)    
  429. XD *xd;
  430. XParams *xp;
  431. {   int ypos = XPOS;
  432.  
  433.     for (i = 0; i < xp->itns; i++) {
  434.     XDrawImageString16(xd->display, xd->window, fontgc, XPOS, ypos,
  435.         buffer16[i], (int) xp->size);
  436.     ypos += fontheight; 
  437.     if (ypos > (DISPHEIGHT - fontheight)) 
  438.         ypos = XPOS;
  439.     }
  440. }
  441.  
  442. int i_ImageText16(xd, xp)    
  443. XD *xd;
  444. XParams *xp;
  445. { i_PolyText16(xd, xp); }
  446.  
  447. int c_ImageText16(xd, xp)    
  448. XD *xd;
  449. XParams *xp;
  450. { c_PolyText16(xd, xp); }
  451.